package com.bw.controller;


import com.bw.bean.User;
import com.bw.service.UserService;
import com.bw.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("list.do")
    public String list(@RequestParam(value = "cpage",defaultValue = "1")Integer cpage, Model model,
                       String mohu, String startTime, String endTime, Integer shengid, Integer shiid, Integer xianid){
        Map map = new HashMap();
        map.put("mohu",mohu);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("shengid",shengid);
        map.put("shiid",shiid);
        map.put("xianid",xianid);
        PageHelper.startPage(cpage,2);
        List list = userService.list(map);
        PageInfo pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    @RequestMapping("citylist.do")
    @ResponseBody
    public Object citylist(Integer pid){
        List list = userService.citylist(pid);
        return list;
    }
    @RequestMapping("toadd.do")
    public String add(){
        return "add";
    }
    @RequestMapping("login.do")
    public String login(User user, HttpSession session){
        User user1 = userService.login(user);
        if(user1!=null){
            session.setAttribute("user",user1);
            return "redirect:list.do";
        }else{
            return "redirect:index.jsp";
        }
    }

    @RequestMapping("add.do")
    @ResponseBody
    public Object add(User user, HttpSession session, MultipartFile photo){
        if(photo.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            user.setPic(upload);
        }
        int add = userService.add(user);
        return add;
    }



}
